package com.z.jdbc_demo;
import java.sql.*;

public class JdbcExample2 {
    public static void main(String[] args) throws ClassNotFoundException, SQLException {
        // 1.注册驱动
        Class.forName("com.mysql.cj.jdbc.Driver");
        // 2.获取数据库连接
        Connection connection = DriverManager.getConnection("jdbc:mysql://localhost/zk?user=root&password=123456");
        // 3.创建Statement
        PreparedStatement preparedStatement = connection.prepareStatement("select count(0) as count from t_student");
        // 4.执行编写的SQL，获取结果
        ResultSet resultSet = preparedStatement.executeQuery();
        // 5.处理结果
        while (resultSet.next()) {
            int count = resultSet.getInt("count");
            System.out.println("总记录数：" + count);
        }
        // 6.释放资源
        resultSet.close();
        preparedStatement.close();
        connection.close();
    }
}
